package com.yj.transport.service;

import com.yj.contract.dto.ContractChangeStatusDto;
import com.yj.oa.entity.system.OAUserInfo;
import com.yj.oa.mapper.OAContractMapper;
import com.yj.oa.mapper.OAUserInfoMapper;
import com.yj.oa.service.OAInfoService;
import com.yj.transport.entity.TransportInfo;
import com.yj.transport.entity.TransportJournal;
import com.yj.transport.mapper.TransportInfoMapper;
import com.yj.transport.mapper.TransportJournalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhengguanyong on 2017/12/6.
 */
@Service
public class TransportInfoService {

    @Autowired
    private TransportInfoMapper transportInfoMapper;

    @Autowired
    OAUserInfoMapper oaUserInfoMapper;

    @Autowired
    TransportJournalService transportJournalService;

    @Autowired
    OAContractMapper oaContractMapper;

    @Autowired
    OAInfoService oaInfoService;

    @Autowired
    private TransportJournalMapper transportJournalMapper;

    @Transactional
    public void createTransportInfo(TransportInfo transportInfo) {

        transportInfo.setArrive_place(transportInfo.getStart_place());
        transportInfo.setArrive_time(transportInfo.getDeliver_time());
        transportInfo.setLongitude(transportInfo.getStart_longitude());
        transportInfo.setLatitude(transportInfo.getStart_latitude());
        transportInfo.setPlace_type(0);
        transportInfoMapper.createTransportInfo(transportInfo);
        TransportJournal transportJournal = new TransportJournal();
        System.out.println("transportInfo.getId():"+transportInfo.getId());
        transportJournal.setTransport_info_id(transportInfo.getId());
        transportJournal.setPlace_type(0);
        transportJournal.setArrive_place(transportInfo.getStart_place());
        transportJournal.setLatitude(transportInfo.getStart_latitude());
        transportJournal.setLongitude(transportInfo.getStart_longitude());
        transportJournal.setArrive_time(transportInfo.getDeliver_time());
        transportJournal.setTransport_type(transportInfo.getTransport_type());
//        transportJournalService.createTransportJournal(transportJournal);
        transportJournalMapper.createTransportJournal(transportJournal);

        ContractChangeStatusDto changeStatusDto = new ContractChangeStatusDto();
        changeStatusDto.setMain_id(transportInfo.getMain_id());
        changeStatusDto.setStatus(2);

        oaContractMapper.updateSystemStatus(changeStatusDto);
    }

    public TransportInfo getTransportInfoDetailById(Integer id) {
        return transportInfoMapper.getTransportInfoDetailById(id);
    }

    public List<TransportInfo> getTransportInfoListByMainId(String main_id) {
        return transportInfoMapper.getTransportInfoListByMainId(main_id);
    }

    public int updateOwnership(Integer id, String ownership) {
        return transportInfoMapper.updateOwnership(id, ownership);
    }

    public int goodsAcceptance(Integer id, int acceptance_status, String ownership) {
        return transportInfoMapper.goodsAcceptance(id, acceptance_status, ownership);
    }

    public List<TransportInfo> getDynamicInfoListByUserId(String token) {
        List<TransportInfo> transportInfoList = new ArrayList<>();
        OAUserInfo oaUserInfo = oaInfoService.getOAUserByToken(token);
        int role_level = oaUserInfo.getRole_level().getValue();
        if (role_level == 1) {
            transportInfoList = transportInfoMapper.getDynamicInfoListByUserId(oaUserInfo.getUser_id());
        } else if (role_level == 2 || role_level == 3 || role_level == 4) {
            Long departmentID = oaInfoService.findDepartmentIDByUserID(oaUserInfo.getUser_id());
            List<String> userIDList = new ArrayList<>();
            oaInfoService.findAllUserForDepartment(departmentID,userIDList);
            transportInfoList = transportInfoMapper.getDynamicInfoListByUserIdList(userIDList);
        } else if (role_level == 5) {

        }
        return transportInfoList;
    }

    public List<TransportInfo> getDynamicInfoListByNone() {
        return transportInfoMapper.getDynamicInfoListByNone();
    }

    public List<String> searchDynamicInfoByPlateNumber(String plate_number) {
        return transportInfoMapper.searchDynamicInfoByPlateNumber(plate_number);
    }

    public TransportInfo getDynamicInfoByPlateNumber(String plate_number) {
        return transportInfoMapper.getDynamicInfoByPlateNumber(plate_number);
    }

    public List<TransportJournal> getDynamicJournalByPlateNumber(String plate_number) {
        TransportInfo transportInfo =  transportInfoMapper.getDynamicInfoByPlateNumber(plate_number);
        return transportJournalService.getTransportJournalListByInfoId(transportInfo.getId());
    }

    public Map<String, Integer> countDynamicInfoGuest() {
        Map<String, Integer> countDynamicInfo = new LinkedHashMap<>();
        countDynamicInfo.put("train", transportInfoMapper.countDynamicInfoGuest(2));
        countDynamicInfo.put("boat", transportInfoMapper.countDynamicInfoGuest(3));
        countDynamicInfo.put("car", transportInfoMapper.countDynamicInfoGuest(1));
        countDynamicInfo.put("goods_weight", transportInfoMapper.sumGoodsWeightDynamicInfoGuest());

        return countDynamicInfo;
    }

    public Map<String, Integer> countDynamicInfo(String token) {
        Map<String, Integer> countDynamicInfo = new LinkedHashMap<>();
        OAUserInfo oaUserInfo = oaInfoService.getOAUserByToken(token);
        int role_level = oaUserInfo.getRole_level().getValue();
        String user_id = oaUserInfo.getUser_id();
        if (role_level == 1) {
            countDynamicInfo.put("train", transportInfoMapper.countDynamicInfo(user_id, 2));
            countDynamicInfo.put("boat", transportInfoMapper.countDynamicInfo(user_id, 3));
            countDynamicInfo.put("car", transportInfoMapper.countDynamicInfo(user_id, 1));
            countDynamicInfo.put("goods_weight", transportInfoMapper.sumGoodsWeightDynamicInfo(user_id));
        } else if (role_level == 2 || role_level == 3 || role_level == 4) {
            Long departmentID = oaInfoService.findDepartmentIDByUserID(oaUserInfo.getUser_id());
            List<String> userIDList = new ArrayList<>();
            oaInfoService.findAllUserForDepartment(departmentID,userIDList);
            countDynamicInfo.put("train", transportInfoMapper.countDynamicInfoLeader(userIDList, 2));
            countDynamicInfo.put("boat", transportInfoMapper.countDynamicInfoLeader(userIDList, 3));
            countDynamicInfo.put("car", transportInfoMapper.countDynamicInfoLeader(userIDList, 1));
            countDynamicInfo.put("goods_weight", transportInfoMapper.sumGoodsWeightDynamicInfoLeader(userIDList));
        } else if (role_level == 5) {

        }
        return countDynamicInfo;
    }

    public BigDecimal sumGoodsWeight(String main_id) {
        return transportInfoMapper.sumGoodsWeight(main_id);
    }

    public BigDecimal sumAmount(String main_id) {
        return transportInfoMapper.sumAmount(main_id);
    }

    public boolean isAllAcceptanceDone(String main_id) {
        if (transportInfoMapper.isAllAcceptanceDone(main_id) > 0) {
            return false;
        } else {
            return true;
        }
    }

    /*
    public List<TransportInfo> getDynamicInfoListByUserIdLeader(OAUserInfo oaUserInfo) {
        List<TransportInfo> transportInfoList = new ArrayList<>();
        switch (oaUserInfo.getRole_level()) {
            case BD_LOW:
                transportInfoList = transportInfoMapper.getDynamicInfoListByTeam(oaUserInfo.getTeam());
                break;
            case BD_HIGH:
                transportInfoList = transportInfoMapper.getDynamicInfoListByDivision(oaUserInfo.getDivision());
                break;
            case BU:
                transportInfoList = transportInfoMapper.getDynamicInfoListByCorp(oaUserInfo.getCorp());
                break;
        }
        return transportInfoList;
    }

    public Map<String,Integer> countDynamicInfoLeader(OAUserInfo oaUserInfo) {
        Map<String,Integer> countDynamicInfo = new LinkedHashMap<>();
        switch (oaUserInfo.getRole_level()) {
            case BD_LOW:
                countDynamicInfo.put("train",transportInfoMapper.countDynamicInfoByTeam(oaUserInfo.getTeam(),2));
                countDynamicInfo.put("boat",transportInfoMapper.countDynamicInfoByTeam(oaUserInfo.getTeam(),3));
                countDynamicInfo.put("car",transportInfoMapper.countDynamicInfoByTeam(oaUserInfo.getTeam(),1));
                countDynamicInfo.put("goods_weight",transportInfoMapper.sumGoodsWeightDynamicInfoByTeam(oaUserInfo.getTeam()));
                break;
            case BD_HIGH:
                countDynamicInfo.put("train",transportInfoMapper.countDynamicInfoByDivision(oaUserInfo.getDivision(),2));
                countDynamicInfo.put("boat",transportInfoMapper.countDynamicInfoByDivision(oaUserInfo.getDivision(),3));
                countDynamicInfo.put("car",transportInfoMapper.countDynamicInfoByDivision(oaUserInfo.getDivision(),1));
                countDynamicInfo.put("goods_weight",transportInfoMapper.sumGoodsWeightDynamicInfoByDivision(oaUserInfo.getDivision()));
                break;
            case BU:
                countDynamicInfo.put("train",transportInfoMapper.countDynamicInfoByCorp(oaUserInfo.getCorp(),2));
                countDynamicInfo.put("boat",transportInfoMapper.countDynamicInfoByCorp(oaUserInfo.getCorp(),3));
                countDynamicInfo.put("car",transportInfoMapper.countDynamicInfoByCorp(oaUserInfo.getCorp(),1));
                countDynamicInfo.put("goods_weight",transportInfoMapper.sumGoodsWeightDynamicInfoByCorp(oaUserInfo.getCorp()));
                break;
        }
        return countDynamicInfo;
    }
    */

}
